package com.bjtime.designer.widget

import android.content.Context
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Typeface
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.widget.TextView
import com.bjtime.designer.R
import com.bjtime.designer.attributes.BaseAttr
import com.bjtime.designer.attributes.TextAttr
import com.bjtime.designer.attributes.ViewAttr
import com.bjtime.designer.utils


/**
 * 注意：
 *  1.PRINT指令打印文本,字体尺寸范围为[1-10， 1-10] 也就是缩放倍数, 无法自由调整字体尺寸！
 *  更无法调整字体样式(例如使用花式字体),所以,此处使用BITMAP指令进行打印.考虑计算PX->MM->DOT之后,
 *  打印非常完美,而且不用考虑编码问题,
 *
 *  经过以上考虑,现得出一个结论, TEXT指令,毫无卵用！辣鸡.
 */

class TextView2 @JvmOverloads constructor (
    context: Context,
    content: String,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : BaseLabelView (context, attrs, defStyleAttr) {


    interface TextView2AttrChangeListener {
        fun onTextChange (s:String)
        fun onSizeChange (size:Int)
    }
    private var listener : TextView2AttrChangeListener? = null
    fun setChangeListener (l:TextView2AttrChangeListener) {
        listener = l
    }

    private var inflater            : LayoutInflater
    private var mLayoutView         : TextView
    private val TagViewName = "TextView2"
    private var des_text            : String = "此处输入要输入的文本"
    private var des_textsize        : Int  = 0
    private var des_text_bold       : Boolean = false
    private var des_text_italics    : Boolean = false
    private var des_text_underline  : Boolean = false
    private var des_text_deleteline : Boolean = false

    fun setBold (b:Boolean) {
        des_text_bold = b
        updateStyle ()

    }

    fun getBold () : Boolean {
        return des_text_bold
    }

    fun setItalics (b:Boolean) {
        des_text_italics = b
        updateStyle ()
    }

    fun getItalics () : Boolean {
        return des_text_italics
    }

    private fun updateStyle () {
        if (mLayoutView != null) {

            var flags = 0
            if (des_text_underline) {
                flags += Paint.UNDERLINE_TEXT_FLAG
            }

            if (des_text_deleteline) {
                flags += Paint.STRIKE_THRU_TEXT_FLAG or Paint.ANTI_ALIAS_FLAG
            }

            var style = 0
            if (des_text_bold) {
                style += Typeface.BOLD
            }
            if (des_text_italics) {
                style += Typeface.ITALIC
            }
            mLayoutView.setTypeface(Typeface.defaultFromStyle(style))
            mLayoutView.getPaint().flags = flags
        }
    }

    fun setUnderline (b:Boolean) {
        des_text_underline = b
        updateStyle ()
        mLayoutView.invalidate()
    }

    fun getUnderline () : Boolean {
        return des_text_underline
    }

    fun setDeleteline (b:Boolean) {
        des_text_deleteline = b
        updateStyle ()
        mLayoutView.invalidate()
    }

    fun getDeleteline () : Boolean {
        return des_text_deleteline
    }


    fun setReverse (b :Boolean) {
        des_reversed = b
        if (b) {
            setBackgroundColor(Color.BLACK)
            mLayoutView.setTextColor(Color.WHITE)
        } else {
            setBackgroundColor(Color.TRANSPARENT)
            mLayoutView.setTextColor(Color.BLACK)
        }
    }

    fun getReverse () : Boolean {
        return des_reversed
    }

    init {
        des_text=content
        des_viewtype = ViewAttr.VIEW_TYPE.TEXT
        if (attrs != null) {
            val a = context.obtainStyledAttributes(attrs, R.styleable.TextView2)
            des_text = if (a.getText(R.styleable.TextView2_text) == null)  des_text else a.getText(R.styleable.TextView2_text).toString()
            a.recycle()
        }
        inflater = LayoutInflater.from(this.context)
        mLayoutView = inflater?.inflate(R.layout.layout_designer_label_textview2, null) as TextView
        this.addView(mLayoutView)

        des_textsize = mLayoutView!!.textSize.toInt()
        mLayoutView.setText(des_text)
        mLayoutView.setTextColor(-0x1000000)

        setSelect (false)
    }


    override fun setSelect(select: Boolean) {
        super.setSelect(select)
        if (!select) {
            mLayoutView.setBackgroundResource(0)
        } else {
            if (!des_lock) {
                mLayoutView.setBackgroundResource(R.drawable.shape_designer_label_border)
            } else {
                mLayoutView.setBackgroundResource(R.drawable.shape_designer_label_border_locked)
            }
        }
    }

    fun setTextSize (size:Int) {
        mLayoutView.textSize =  size.toFloat()
        des_textsize = size
        if (listener != null)
            listener!!.onSizeChange(size)
    }

    fun getTextSize () : Int {
        return mLayoutView.textSize.toInt()
    }

    fun setText (s:String) {
        mLayoutView.text = s
        des_text = s
        if (listener != null)
            listener!!.onTextChange(s)
    }

    fun getText () : String {
        return des_text!!
    }


    override fun getAttr ():TextAttr {
        var attr  = TextAttr ();
        attr.des_id = des_id
        attr.des_left = des_left
        attr.des_top = des_top
        attr.des_width = des_width
        attr.des_height = des_height
        attr.des_lock = des_lock
        attr.des_ptype = des_ptype
        attr.des_rotate = des_rotate
        attr.des_text   = des_text
        attr.des_textsize = des_textsize
        attr.des_reversed = des_reversed
        attr.des_viewtype = ViewAttr.VIEW_TYPE.TEXT

        attr.des_text_bold = des_text_bold
        attr.des_text_italics = des_text_italics
        attr.des_text_underline = des_text_underline
        attr.des_text_deleteline = des_text_deleteline

        attr.prt_bottom = prt_bottom
        attr.prt_right  = prt_right
        attr.prt_top    = prt_top
        attr.prt_left   = prt_left

        return attr
    }


    override fun setAttr (attr: BaseAttr?) {
        attr as TextAttr
        des_id = attr.des_id
        des_left = attr.des_left
        des_top = attr.des_top
        des_width = attr.des_width
        des_height = attr.des_height
        des_lock = attr.des_lock
        des_ptype = attr.des_ptype
        des_rotate = attr.des_rotate
        des_reversed = attr.des_reversed
        des_viewtype = attr.des_viewtype
        des_text = attr.des_text
        des_textsize = attr.des_textsize

        des_text_bold = attr.des_text_bold
        des_text_italics = attr.des_text_italics
        des_text_underline = attr.des_text_underline
        des_text_deleteline = attr.des_text_deleteline
    }

    override fun toString(): String {
        val s = "ViewName    : " + TagViewName  + "\n" +
                "view_id     : " + des_id       + "\n" +
                "view_left   : " + des_left     + "\n" +
                "view_top    : " + des_top      + "\n" +
                "view_width  : " + des_width    + "\n" +
                "view_height : " + des_height   + "\n" +
                "view_islock : " + des_lock     + "\n" +
                "view_select : " + des_select   + "\n" +
                "view_ptype  : " + des_ptype    + "\n" +
                "view_rotate : " + des_rotate   + "\n"
        return s
    }

    override fun onMoved(spac: Int) {
        super.onMoved(spac)
        prt_left = (des_left + utils.dp2px(context, 2f)) / spac
        prt_top = (des_top + utils.dp2px(context, 2f)) / spac
        prt_right = (des_width - utils.dp2px(context, 4f)) / spac
        prt_bottom = (des_height - utils.dp2px(context, 4f)) / spac
    }

    override fun prepared() {
        updateStyle()
        setText(des_text)
        setTextSize(des_textsize)
        setReverse (des_reversed)
    }
}